package edu.cs346.group5.ui.forumpages;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.mysql.jdbc.Connection;

import edu.cs346.group5.ui.MenuFactory;
import edu.cs346.group5.ui.ResultsTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class search_topic extends JFrame {

	private JPanel contentPane;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public search_topic() {
		setTitle("Search All Topics");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[][grow]", "[][][]"));
		
		JLabel lblTerm = new JLabel("Term:");
		contentPane.add(lblTerm, "cell 0 0,alignx trailing");
		
		setJMenuBar(MenuFactory.getMenu(this));
		
		textField = new JTextField();
		contentPane.add(textField, "cell 1 0,growx");
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("Search");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String dbName = "jdbc:mysql://cs341.cs.uwosh.edu:4381/cs341g5";
				try
			      {
			          Class.forName("com.mysql.jdbc.Driver");  // register the driver
			          java.sql.Connection conn = DriverManager.getConnection(dbName,"cs341g5","d632449");
			          String toSearch = textField.getText();
			          String quote = "\"";
			          String query = "SELECT posted_date,category_name,content,rating from Posts INNER JOIN Categories on Categories.id =  Posts.category where " +
			          		"category LIKE '%"+toSearch+"%' OR content LIKE '%"+toSearch+"%'";

        

			         
			  
			          java.sql.Statement st = conn.createStatement();
			          ResultSet rs = st.executeQuery(query);
			          ArrayList<String[]> rst = new ArrayList<String[]>();
			          String[] set = new String[4];
			          set[0] = "Date";
			          set[1] = "Category";
			          set[2] = "Content";
			          set[3] = "Rating";
			          rst.add(set);
	
			          while (rs.next()) {
			            String date = rs.getString("posted_date");
			            String cat = rs.getString("category_name");
			            String cont = rs.getString("content");
			            String rat = rs.getString("rating");
			            set = new String[4];
			            set[0] = date;
			            set[1] = cat;
			            set[2] = cont;
			            set[3] = rat;
			            rst.add(set);
			          }   
			          conn.close();
			          ResultsTable.main(rst, true);
			      }
			      catch (SQLException e1) {System.out.println("SQL Error " + e1.getMessage());}
			      catch (Exception e1) {System.out.println("General Error " + e1.getMessage());}
				

				
				
			}
		});


		contentPane.add(btnNewButton, "cell 1 2,alignx right");
	}

}
